﻿using IRUM.Core;
using IRUM.XingAPI;
using System;
using System.Windows.Forms;

namespace IRUM
{
    public partial class LoginDialog : Form
    {
        public string UserName = string.Empty;
        public string Password = string.Empty;
        public string CertPass = string.Empty;
        private SessionCtrl m_session;

        public LoginDialog(SessionCtrl session)
        {
            InitializeComponent();
            this.m_session = session;
            m_session.OnLoginResultReceived += m_session_OnLoginResultReceived;
        }

        private void LoginDialog_Load(object sender, EventArgs e)
        {
            txtLoginID.Text = "nam1397";
            txtLoginPass.Text = "dlfnfl";
            txtCertPass.Text = "full5117";

            ProcessOK();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ProcessOK();
        }

        private void txtCertPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ProcessOK();
            }
        }

        private void ProcessOK()
        {
            if (txtLoginID.Text.Length == 0)
            {
                MessageBox.Show("로그인ID를 입력하십시오.");
                return;
            }

            if (txtLoginPass.Text.Length == 0)
            {
                MessageBox.Show("로그인 비밀번호를 입력하십시오.");
                return;
            }

            if (txtCertPass.Text.Length == 0)
            {
                MessageBox.Show("인증서 비밀번호를 입력하십시오.");
                return;
            }



            //로그인
            m_session.m_Session.ConnectServer("hts.etrade.co.kr", 20001);
            if (m_session.m_Session.IsConnected() == true)
            {
                m_session.m_Session.Login(txtLoginID.Text, txtLoginPass.Text, txtCertPass.Text, 0, true);
            }
        }

        void m_session_OnLoginResultReceived(object s, loginArgs e)
        {
            if (e.code.Equals("0000") == true)
            {
                GeneralManager.Instance.m_Session = m_session.m_Session;
                //GetAccountList();

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("[로그인에러] " + e.code + ", " + e.message);
            }
        }

        private void txtLoginPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtCertPass.Focus();
            }
        }

        private void txtLoginID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtLoginPass.Focus();
            }
        }

        #region Account Data

        private void GetAccountList()
        {
            int AccountCount = m_session.m_Session.GetAccountListCount();

            for (int i = 0; i < AccountCount; i++)
            {
                GeneralManager.Instance.AccountList.Add(m_session.m_Session.GetAccountList(i));
            }
        }

        #endregion
    }
}
